from es import setplayerprop, getplayerprop, doblock
from random import randint
from gamethread import delayed
from superdice.libs.diceConfigs import configFile
from superdice.superdice import registerRoll, unregisterRoll, players, getGameTime

def es_map_start(ev):
    cfgData.execute()

def unload():
    doblock('corelib/noisy_off')
    unregisterRoll(diceRoll)

def player_spawn(ev):
    players[ev['userid']]['wpnjam'] = False

def weapon_fire(ev):
    if not randint(0, 100) <= int(chance):
        return

    userid = str(ev['userid'])
    if not players[userid]['wpnjam']:
        return

    setplayerprop(userid, 'CBaseCombatCharacter.bcc_localdata.m_flNextAttack', getGameTime() + 2)

def diceRoll(userid, player, eplayer):
    players[userid]['wpnjam'] = True

cfgData = configFile('dice_weaponjam')
chance = cfgData.cvar('superdice_cfg_weaponjam_chance', 10, 'What is the chance for weapon jamming to happen?')
cfgData.write()
cfgData.execute()

doblock('corelib/noisy_on')

registerRoll(diceRoll, 'weaponjam')
